<?php

namespace Home\Controller;

use Think\Controller;

class UserInfoController extends CommnController
{

    public function __construct()
    {
        parent::__construct();
    }

    public function user_list_info()
    {
        $search = I('get.user');
        if (empty($search) || $search == '') {
            $this->display();
            die;
        }
        $this->roles = role(I('rid'));
        $this->rid = I('rid');
        $this->user_content = $search;

        if (!empty($search)) {
            $where = [];
            $where['mobile|truename'] = array('like', '%' . $search . '%');
        }
        $tol = M('user', '', 'USER')->where($where)->count();
        $row = '12';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $user = M('user', '', 'USER')->where($where)->field('vid,id,mobile,truename,cert_status,id_card,ctime')->LIMIT($page->listfirst, $page->listRows)->order('ctime desc')->select();
        foreach ($user as $k => $v) {
            if ($v['cert_status'] == '1') {
                $user[$k]['cert_status'] = '未认证';
            } else if ($v['cert_status'] == '2') {
                $user[$k]['cert_status'] = '已通过';
            } else if ($v['cert_status'] == '3') {
                $user[$k]['cert_status'] = '未通过';
            }
            $len = strlen($v['id_card']);

            if (!empty($len)) {
                $start = substr($v['id_card'], 0, 6);
                $end = substr($v['id_card'], $len - 4, $len);
                $user[$k]['id_card1'] = $start . '*******' . $end;
            }
        }
        if (empty($search)) {
            $user = '';
            $tol = 0;
        }
        $this->page = I('page');
        $this->assign(array(
            'user' => $user,
            'fpage' => $fpage,
            'tol' => $tol
        ));

        $this->display();
    }

    public function user_edit()
    {
        $vid = I('vid');
        $name = I('name');
        $card = I('card');
        $where = [];
        $where['vid'] = array('eq', $vid);
        $data = [];
        $data['truename'] = $name;
        $data['id_card'] = $card;
        $user = M('user', '', 'USER')->where($where)->save($data);
        if ($user) {
            //操作日志
            $str = array('aid' => $_SESSION['id'], 'content' => '修改用户' . $vid . '姓名身份证信息为' . $name . $card, 'ctime' => time());
            M('admin_log', '', 'KEFU')->add($str);
            $this->ajax('1001', '修改成功');
        } else {
            $this->ajax('1002', '修改失败');
        }
    }

    public function user_list_details()
    {
        $this->page1 = I('page1');
        $this->rid = I('rid');
        $this->key = I('key');
        $this->utype = I('utype');
        $vid = I('get.vid');
        $user = M('user', '', 'USER')->where(['vid' => $vid])->field('alipay_verify,work_verify,waitdays,vid,mobile,truename,id_card,icon,address,status,waitdays,statustype,province,city,residence_time,
        ismarry,degree,checkstatus,work_cert,cert_status,zhima_verify,contact_verify,cert_time,ctime,sex,taobao_verify,
        usermsg_verify,mobile_verify,shebao_verify,jd_verify,credit_verify,check_type,money,quota,balance,freeze_balance')->find();

        //支付宝认证1未认证，2认证中，3已认证，4认证失败

        switch ($user['alipay_verify']) {
            case 1:
                $user['alipay_verify'] = '未认证';
                break;
            case 2:
                $user['alipay_verify'] = '认证中';
                break;
            case 3:
                $user['alipay_verify'] = '已认证';
                break;
            case 4:
                $user['alipay_verify'] = '认证失败';
                break;
        }

        switch ($user['work_verify']) {
            case 1:
                $user['work_verify'] = '认证';
                break;
            case 2:
                $user['work_verify'] = '未认证';
                break;
            case 0:
                $user['work_verify'] = '未认证';
                break;
        }

        if ($user['waitdays'] != 0) {
            $user['waitdays'] = round(($user['waitdays'] - time()) / (3600 * 24));
        }

        if ($user['waitdays'] <= 0) {
            $user['waitdays'] = 0;
        }

        $user['mobilefirm'] = M('user_login', '', 'USER')->where(['vid' => $vid])->getfield('mobilefirm');
        //拉黑用户的 拉黑原因
        if ($user['status'] == '3') {
            $user['user_status'] = '3';
            $user['black_reason'] = M('user_blacklist', '', 'URGE')->where(['vid' => $user['vid'], 'status' => '1'])->getfield('content');
        } else {
            $user['user_status'] = $user['status'];
            $user['black_reason'] = '';
        }

        $photo = M('user_idcard_img', '', 'USER')->where(['vid' => $vid])->getfield('photo');
        if (!empty($photo)) {
            $user['icon'] = $photo;
        } else {
            $user['icon'] = 'http://nkf.56gxxj.com/Public/default_icon.jpg';
        }
        //用户预约状态
        $pre_borrow = M('user_appointment', '', 'CAIWU')->where(['vid' => $user['vid']])->field('status')->select();
        $pre_arr = array_column($pre_borrow, 'status');
        if (in_array('1', $pre_arr)) {
            $pre_status = '待打款';
        } else {
            $pre_status = '无预约';
        }

        //用户标识欺诈标识，1正常，2黑户
        $mark = M('user_login', '', 'USER')->where(['vid' => $user['vid']])->getfield('mark');
        if ($mark == 1) {
            $user['mark'] = '正常';
        } else if ($mark == 2) {
            $user['mark'] = '黑户';
        }
//        var_dump($user);die;
        //运营商认证时间 ZX_R
        $time = M('mobile_basicinfo', '', 'ZX_R')->where(['vid' => $user['vid']])->field('register_time')->find();
        $cert_time = $user['cert_time'];

        if ($time['register_time'] <= 0) {
            $user['register_time'] = '';
        } else {
//            $user['register_time']=1;
            $user['register_time'] = date('Y-m-d H:i', $time['register_time']);
        }

        if (empty($user['cert_time'])) {
            $user['cert_time'] = '';
        } else {
            $user['cert_time'] = date('Y-m-d H:i', $user['cert_time']);
        }

        //身份证正反面图片
        $pic = M('user_idcard_img', '', 'USER')->where(['vid' => $user['vid']])->field('front,behind')->find();
        $user['font'] = $pic['front'];
        $user['behind'] = $pic['behind'];

        $user['ctime'] = date('Y-m-d H:i', $user['ctime']);
        if ($user['cert_status'] == 2) {
            $user['cert_status'] = '通过';
        } else {
            $user['cert_status'] = '未通过';
        }
        if ($user['sex'] == 1) {//用户性别
            $user['sex'] = '男';
        } else if ($user['sex'] == 2) {
            $user['sex'] = '女';
        } else if ($user['sex'] == 0) {
            $user['sex'] = '未知';
        }

        if ($user['status'] == 1) {//当前状态
            $user['status'] = '正常';
        } else if ($user['status'] == 2) {
            $user['status'] = '锁定';
        } else if ($user['status'] == 3) {
            $user['status'] = '黑名单';
        }

        if ($user['check_type'] == 1) {//操作类型
            $user['check_type'] = '无操作';
        } else if ($user['check_type'] == 2) {
            $user['check_type'] = '机器审核';
        } else if ($user['check_type'] == 3) {
            $user['check_type'] = '人工审核';
        }

        if ($user['statustype'] == 1) {//审核类型
            $user['statustype'] = '信息认证审核';
        } else if ($user['statustype'] == 2) {
            $user['statustype'] = '贷前审核';
        } else {
            $user['statustype'] = '无';
        }

        if ($user['checkstatus'] == 1) {//当前审核状态
            $user['checkstatus'] = '待审核';
        } else if ($user['checkstatus'] == 2) {
            $user['checkstatus'] = '正在审核状态';
        } else if ($user['checkstatus'] == 3) {
            $user['checkstatus'] = '完成审核';
        }

        if ($user['work_cert'] == 1) {//工作认证
            $user['work_cert'] = '未认证';
        } else if ($user['work_cert'] == 2) {
            $user['work_cert'] = '已认证';
        }

        if ($user['zhima_verify'] == 1) {//芝麻认证
            $user['zhima_verify'] = '通过';
        } else if ($user['zhima_verify'] == 0) {
            $user['zhima_verify'] = '未通过';
        }

        if ($user['contact_verify'] == 1) {//紧急联系人
            $user['contact_verify'] = '已完成';
        } else if ($user['contact_verify'] == 0) {
            $user['contact_verify'] = '未完成';
        }

        if ($user['usermsg_verify'] == 1) {//用户信息
            $user['usermsg_verify'] = '已完成';
        } else if ($user['usermsg_verify'] == 0) {
            $user['usermsg_verify'] = '未完成';
        }

        if ($user['mobile_verify'] == 1) {//运营商认证
            $user['mobile_verify'] = '未认证';
        } else if ($user['mobile_verify'] == 2) {
            $user['mobile_verify'] = '认证中';
        } else if ($user['mobile_verify'] == 3) {
            $user['mobile_verify'] = '认证成功';
        } else if ($user['mobile_verify'] == 4) {
            $user['mobile_verify'] = '认证失败';
        }
        switch ($user['taobao_verify']) {
            case 1:
                $user['taobao_verify'] = '未认证';
                break;
            case 2:
                $user['taobao_verify'] = '认证中';
                break;
            case 3:
                $user['taobao_verify'] = '认证成功';
                break;
            case 4:
                $user['taobao_verify'] = '认证失败';
                break;
        }

        if ($user['shebao_verify'] == 1) {//社保认证
            $user['shebao_verify'] = '未认证';
        } else if ($user['shebao_verify'] == 2) {
            $user['shebao_verify'] = '认证中';
        } else if ($user['shebao_verify'] == 3) {
            $user['shebao_verify'] = '认证成功';
        } else if ($user['shebao_verify'] == 4) {
            $user['shebao_verify'] = '认证失败';
        }


        if ($user['jd_verify'] == 1) {//社保认证
            $user['jd_verify'] = '未认证';
        } else if ($user['jd_verify'] == 2) {
            $user['jd_verify'] = '认证中';
        } else if ($user['jd_verify'] == 3) {
            $user['jd_verify'] = '认证成功';
        } else if ($user['jd_verify'] == 4) {
            $user['jd_verify'] = '认证失败';
        }


        if ($user['credit_verify'] == 1) {//信用卡认证
            $user['credit_verify'] = '未认证';
        } else if ($user['credit_verify'] == 2) {
            $user['credit_verify'] = '认证中';
        } else if ($user['credit_verify'] == 3) {
            $user['credit_verify'] = '认证成功';
        } else if ($user['credit_verify'] == 4) {
            $user['credit_verify'] = '认证失败';
        }

        $login = M('user_login', '', 'USER')->where(['vid' => $user['vid']])->field('mobilefirm,register_ip')->find();
        $user['mobilefirm'] = $login['mobilefirm'];
        $user['register_ip'] = $login['register_ip'];

        $type = I('get.type', '1');
//        $type = 7;
        $id = I('get.id');

        if ($type == 1) {//借款记录
            $borrow = M('user_borrow', '', 'CAIWU');
            $tol = M('user_borrow', '', 'CAIWU')->join('b join rate a on b.rate_id=a.id')->where(['vid' => $user['vid']])->count();
            $row = '10';
            $page = new PageController($tol, $row);
            $fpage = $page->fpage();

            $data = M('user_borrow', '', 'CAIWU')->join('b join rate a on b.rate_id=a.id')->where(['vid' => $user['vid']])
                ->field('b.borrow_type,b.rate_id,b.borrow_no,b.money,b.fee,b.is_success,b.ctime,b.get_time,b.status,a.type')
                ->order('b.ctime desc')->LIMIT($page->listfirst, $page->listRows)->select();
            foreach ($data as $k => $v) {
                $pay_time = M('user_repayment', '', 'CAIWU')->where(['borrow_no' => $v['borrow_no']])->getfield('repay_time');
                $data[$k]['repay_time'] = date('Y-m-d H:i', $pay_time);
                if ($v['is_success'] == 1) {
                    $data[$k]['is_success'] = '打款中';
                } else if ($v['is_success'] == 2) {
                    $data[$k]['is_success'] = '成功到账';
                } else if ($v['is_success'] == 3) {
                    $data[$k]['is_success'] = '打款失败';
                }

                if ($v['status'] == 1) {
                    $data[$k]['status'] = '未还款';
                } else if ($v['status'] == 2) {
                    $data[$k]['status'] = '已还款';
                }
                $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
                if ($v['get_time'] > 100) {
                    $data[$k]['get_time'] = date('Y-m-d H:i', $v['get_time']);
                } else {
                    $data[$k]['get_time'] = '-';
                }

                switch ($v['borrow_type']) {
                    case 1:
                        $data[$k]['borrow_type'] = "正常借款";
                        break;
                    case 2:
                        $data[$k]['borrow_type'] = "不限额借款";
                        break;
                    case 3:
                        $data[$k]['borrow_type'] = "购买旅游产品";
                        break;
                    case 4:
                        $data[$k]['borrow_type'] = "退款旅游产品";
                        break;
                    case 5:
                        $data[$k]['borrow_type'] = "购买硅谷堂产品";
                        break;
                    case 6:
                        $data[$k]['borrow_type'] = "购买聚一场产品";
                        break;
                }


            }

        }

        if ($type == 2) {

            $user_repayment = M('user_repayment', '', 'CAIWU');
            $tol = $user_repayment->where(['vid' => $user['vid']])->count();
            $row = '10';
            $page = new PageController($tol, $row);
            $fpage = $page->fpage();

            $data = M('user_repayment', '', 'CAIWU')->where(['vid' => $user['vid']])->LIMIT($page->listfirst, $page->listRows)
                ->field('borrow_no,repayment_no,repay_money,repay_time,real_time,type,status,rate_id,have_repay_money,renewal_money,fee')->order('repay_time desc,real_time desc')->select();
            foreach ($data as $k => $v) {
                $over_status = M('overdue_order', '', 'CAIWU')->where(['repayment_no' => $v['repayment_no']])->getfield('status');

                if ($v['is_success'] == 1) {
                    $data[$k]['is_success'] = '打款中';
                } else if ($v['is_success'] == 2) {
                    $data[$k]['is_success'] = '成功到账';
                } else if ($v['is_success'] == 3) {
                    $data[$k]['is_success'] = '打款失败';
                }

                if ($v['type'] == 1) {
                    $data[$k]['type'] = '主动打款';
                } else if ($v['type'] == 2) {
                    $data[$k]['type'] = '自动还款';
                } else if ($v['type'] == 3) {
                    $data[$k]['type'] = '手动还款';
                } else if ($v['type'] == 4) {
                    $data[$k]['type'] = '支付宝还款';
                } else if ($v['type'] == 5) {
                    $data[$k]['type'] = '聚合平台支付还款';
                }

                if ($v['status'] == 1) {
                    $data[$k]['status'] = '待还款';
                } else if ($v['status'] == 2) {
                    $data[$k]['status'] = '还款中';
                } else if ($v['status'] == 3) {
                    $data[$k]['status'] = '还款成功';
                } else if ($v['status'] == 4) {
                    $data[$k]['status'] = '还款失败';
                } else if ($v['status'] == 5) {
                    $data[$k]['status'] = '取消还款';
                }
                $data[$k]['pay_status'] = $v['status'];
                if ($over_status == '1') {
                    $data[$k]['pay_status'] = '1';
                }


                $data[$k]['repay_time'] = date('Y-m-d H:i', $v['repay_time']);
                if ($v['real_time'] > 0) {
                    $data[$k]['real_time'] = date('Y-m-d H:i', $v['real_time']);
                } else {
                    $data[$k]['real_time'] = '';
                }

                //还款天数
                $num = M('rate', '', 'CAIWU')->where(['id' => $v['rate_id'], 'status' => '1'])->getfield('type');
                $data[$k]['day'] = $num;
            }
        }
        //逾期记录
        if ($type == 3) {

            $user_overdue = M('user_overdue', '', 'URGE');
            $tol = $user_overdue->where(['vid' => $user['vid']])->count();
            $row = '10';
            $page = new PageController($tol, $row);
            $fpage = $page->fpage();


            $data = $user_overdue->where(['vid' => $user['vid']])->LIMIT($page->listfirst, $page->listRows)
                ->field('id,repayment_no,should_time,overdue_day,overdue_money,overdue_time,reduce_money,status')
                ->order('should_time desc,overdue_time desc')->select();
            foreach ($data as $k => $v) {

                if ($v['status'] == 1) {
                    $data[$k]['status'] = '未还款';
                } else if ($v['status'] == 2) {
                    $data[$k]['status'] = '已还款';
                } else if ($v['status'] == 3) {
                    $data[$k]['status'] = '已续期';
                }
                $data[$k]['should_time'] = date('Y-m-d H:i', $v['should_time']);
                if ($v['overdue_time'] == 0) {
                    $data[$k]['overdue_time'] = '';
                } else {
                    $data[$k]['overdue_time'] = date('Y-m-d H:i', $v['overdue_time']);
                }
            }
        }
        //续借记录
        if ($type == 4) {

            $renewal = M('repay_renewal', '', 'CAIWU');
            $tol = $renewal->where(['b.vid' => $user['vid']])->join('b join user_repayment a on b.repayment_no=a.repayment_no')->count();
            $row = '10';
            $page = new PageController($tol, $row);
            $fpage = $page->fpage();
            $data = $renewal->where(['b.vid' => $user['vid']])->join('b join user_repayment a on b.repayment_no=a.repayment_no')
                ->field('a.repay_money,b.orderno,b.repayment_no,b.changetime,b.money,b.type,b.ctime,b.status,b.pay_type')
                ->order('b.ctime desc')->LIMIT($page->listfirst, $page->listRows)->select();
            foreach ($data as $k => $v) {
                if ($v['status'] == 1) {
                    $data[$k]['status'] = '待支付';
                } else if ($v['status'] == 2) {
                    $data[$k]['status'] = '已支付';
                } else if ($v['status'] == 3) {
                    $data[$k]['status'] = '删除';
                } else if ($v['status'] == 4) {
                    $data[$k]['status'] = '已退款';
                }

                if ($v['pay_type'] == 1) {
                    $data[$k]['pay_type'] = '连连支付';
                } else if ($v['pay_type'] == 2) {
                    $data[$k]['pay_type'] = '支付宝支付';
                } else if ($v['pay_type'] == 3) {
                    $data[$k]['pay_type'] = '手动打款';
                } else if ($v['pay_type'] == 4) {
                    $data[$k]['pay_type'] = '聚合平台支付';
                } else if ($v['pay_type'] == 5) {
                    $data[$k]['pay_type'] = '连连自动代扣';
                } else if ($v['pay_type'] == 6) {
                    $data[$k]['pay_type'] = '京东自动代扣';
                } else if ($v['pay_type'] == 7) {
                    $data[$k]['pay_type'] = '京东支付主动还款';
                } else if ($v['pay_type'] == 8) {
                    $data[$k]['pay_type'] = '强制续借，累加本金上';
                }

                if ($v['type'] == 1) {
                    $data[$k]['type'] = '正常续借';
                } else if ($v['type'] == 2) {
                    $data[$k]['type'] = '逾期续借';
                }
                $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
                //续借金额
                $re_money = M('user_repayment', '', 'CAIWU')->join('b join dxd_caiwu.user_borrow a on b.borrow_no=a.borrow_no')->where(['repayment_no' => $v['repayment_no']])->getfield('money');

                $data[$k]['re_money'] = $re_money;
            }
        }

        //练练支付记录
        if ($type == 5) {

            $renewal = M('llzf_record', '', 'CWXX');
            $tol = $renewal->where(['vid' => $user['vid']])->count();
            $row = '10';
            $page = new PageController($tol, $row);
            $fpage = $page->fpage();

            $data = $renewal->where(['vid' => $user['vid']])->LIMIT($page->listfirst, $page->listRows)
                ->field('llzf_orderno,content,real_money,real_time,orderno,type,money,ctime,status')->order('ctime desc')->select();

            foreach ($data as $k => $v) {

                if ($v['status'] == 1) {
                    $data[$k]['status'] = '成功';
                } else if ($v['status'] == 2) {
                    $data[$k]['status'] = '失败';
                } else if ($v['status'] == 3) {
                    $data[$k]['status'] = '未到';
                }

                if ($v['type'] == 1) {
                    $data[$k]['type'] = '借款';
                } else if ($v['type'] == 2) {
                    $data[$k]['type'] = '主动还款';
                } else if ($v['type'] == 3) {
                    $data[$k]['type'] = '自动还款';
                } else if ($v['type'] == 4) {
                    $data[$k]['type'] = '续期';
                } else if ($v['type'] == 5) {
                    $data[$k]['type'] = '人工打款';
                } else if ($v['type'] == 6) {
                    $data[$k]['type'] = '用户提现';
                } else if ($v['type'] == 7) {
                    $data[$k]['type'] = '加盟费';
                }
                $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
                if ($v['real_time'] > 0) {
                    $data[$k]['real_time'] = date('Y-m-d H:i', $v['real_time']);
                } else {
                    $data[$k]['real_time'] = '-';
                }
            }

        }
        //支付宝支付记录
        if ($type == 6) {
            $renewal = M('alipay_record', '', 'CWXX');
            $tol = $renewal->where(['vid' => $user['vid']])->count();
            $row = '10';
            $page = new PageController($tol, $row);
            $fpage = $page->fpage();

            $data = $renewal->where(['vid' => $user['vid']])->LIMIT($page->listfirst, $page->listRows)
                ->field('alipay_orderno,orderno,type,money,ctime,status,content')->order('ctime desc')->select();

            foreach ($data as $k => $v) {

                if ($v['status'] == 1) {
                    $data[$k]['status'] = '成功';
                } else if ($v['status'] == 2) {
                    $data[$k]['status'] = '失败';
                } else if ($v['status'] == 3) {
                    $data[$k]['status'] = '未到';
                }

                if ($v['type'] == 1) {
                    $data[$k]['type'] = '部分还款';
                } else if ($v['type'] == 2) {
                    $data[$k]['type'] = '主动还款';
                } else if ($v['type'] == 3) {
                    $data[$k]['type'] = '续期';
                } else if ($v['type'] == 4) {
                    $data[$k]['type'] = '加盟费';
                }
                $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            }
        }

        //银行卡绑定记录
        if ($type == 7) {
            $renewal = M('user_bank', '', 'CWXX');
            $tol = $renewal->where(['vid' => $user['vid']])->count();
            $row = '10';
            $page = new PageController($tol, $row);
            $fpage = $page->fpage();

            $data = $renewal->where(['vid' => $user['vid']])->LIMIT($page->listfirst, $page->listRows)
                ->field('bank_card,username,bankclass,mobile,status,use_status,ctime')->order('ctime desc')->select();

            foreach ($data as $k => $v) {

                if ($v['status'] == 1) {
                    $data[$k]['status'] = '认证未通过';
                } else if ($v['status'] == 2) {
                    $data[$k]['status'] = '认证通过';
                }

                if ($v['use_status'] == 1) {
                    $data[$k]['use_status'] = '正在使用';
                } else if ($v['use_status'] == 2) {
                    $data[$k]['use_status'] = '已取消绑定';
                }

                if (!empty($v['bank_card'])) {
                    $len = count($v['bank_card']);
                    $str = substr($v['bank_card'], 0, 4);
                    $endstr = substr($v['bank_card'], $len - 5, 4);
                    $data[$k]['bank_card1'] = $str . '***' . $endstr;
                }
                $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            }
        }
        //额度记录
        if ($type == 8) {
            $quota = M('user_quota_record', '', 'CAIWU');
            $tol = $quota->where(['vid' => $user['vid']])->count();
            $row = '10';
            $page = new PageController($tol, $row);
            $fpage = $page->fpage();
            $data = $quota->where(['vid' => $user['vid']])->LIMIT($page->listfirst, $page->listRows)
                ->field('money,content,type,ctime')->order('ctime desc')->select();
            foreach ($data as $k => $v) {
                switch ($v['type']) {
                    case 1:
                        $data[$k]['type'] = '机器修改';
                        break;
                    case 2:
                        $data[$k]['type'] = '人工修改';
                        break;
                    case 3:
                        $data[$k]['type'] = '临时提额';
                        break;
                }
                $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            }
        }

        //奖金记录
        if ($type == 9) {
            $bonus = M('bonus_log', '', 'CAIWU');
            $tol = $bonus->where(['vid' => $user['vid']])->count();
            $row = '10';
            $page = new PageController($tol, $row);
            $fpage = $page->fpage();
            $data = $bonus->where(['vid' => $user['vid']])->LIMIT($page->listfirst, $page->listRows)->field('id', true)->order('ctime desc')->select();
            foreach ($data as $k => $v) {
                switch ($v['type']) {
                    case 1:
                        $data[$k]['type'] = '借款';
                        break;
                    case 2:
                        $data[$k]['type'] = '按时还款';
                        break;
                    case 3:
                        $data[$k]['type'] = '邀请加盟商';
                        break;
                    case 4:
                        $data[$k]['type'] = '加盟商业务分润';
                        break;
                }
                switch ($v['level']) {
                    case 1:
                        $data[$k]['level'] = '直推下家';
                        break;
                    case 2:
                        $data[$k]['level'] = '下家的下家';
                        break;
                }
                $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            }
        }

        //提现记录
        if ($type == 10) {
            $withdraw = M('user_withdraw', '', 'CAIWU');
            $tol = $withdraw->where(['vid' => $user['vid']])->count();
            $row = '10';
            $page = new PageController($tol, $row);
            $fpage = $page->fpage();
            $data = $withdraw->where(['vid' => $user['vid']])->LIMIT($page->listfirst, $page->listRows)->field('id', true)->order('ctime desc')->select();
            foreach ($data as $k => $v) {
                switch ($v['is_success']) {
                    case 0:
                        $data[$k]['is_success'] = '待打款';
                        break;
                    case 1:
                        $data[$k]['is_success'] = '打款中';
                        break;
                    case 2:
                        $data[$k]['is_success'] = '打款成功';
                        break;
                    case 3:
                        $data[$k]['is_success'] = '打款失败';
                        break;
                }
                switch ($v['status']) {
                    case 1:
                        $data[$k]['status'] = '审核中';
                        break;
                    case 2:
                        $data[$k]['status'] = '通过';
                        break;
                    case 3:
                        $data[$k]['status'] = '驳回';
                        break;
                }
                if (!empty($v['get_time'])) {
                    $data[$k]['get_time'] = date('Y-m-d H:i', $v['get_time']);
                } else {
                    $data[$k]['get_time'] = '';
                }
                $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            }
        }

        //优惠券记录
        if ($type == 11) {
            $coupon = M('user_coupon', '', 'CAIWU');
            $tol = $coupon->alias('a')->join('coupon b on a.coupon_id=b.id')->where(['a.vid' => $user['vid']])->count();
            $row = '10';
            $page = new PageController($tol, $row);
            $fpage = $page->fpage();
            $field = 'b.name,a.coupon_id,a.status,a.money,a.ctime,a.endtime';
            $data = $coupon->alias('a')->join('coupon b on a.coupon_id=b.id')->field($field)
                ->where(['a.vid' => $user['vid']])->LIMIT($page->listfirst, $page->listRows)->order('a.ctime desc')->select();;
            foreach ($data as $k => $v) {
                switch ($v['status']) {
                    case 1:
                        $data[$k]['status'] = '未使用';
                        break;
                    case 2:
                        $data[$k]['status'] = '已使用';
                        break;
                    case 3:
                        $data[$k]['status'] = '已过期';
                        break;
                }
                $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
                $data[$k]['endtime'] = date('Y-m-d H:i', $v['endtime']);
            }
        }


        $this->user_type = I('user_type');
        $this->user_content = I('user_content');
//        $this->page=I('page');
        $this->assign(array(
            'user' => $user,
            'id' => $id,
            'vid' => $user['vid'],
            'cert_time' => $cert_time,
            'pre_status' => $pre_status,
            'data' => $data,
            'type' => $type,
            'fpage' => $fpage,
            'tol' => $tol
        ));

        $this->display();

    }


    public function user_money_list()
    {
        $type = I('get.type');
        $id = I('get.id');
        if ($type == 1) {
            $data = M('user_borrow', '', 'CAIWU')->where(['id' => $id])->field('borrow_no,money,fee,is_success,ctime,get_time,status')->select();
        }
        $this->ajaxReturn(['code' => '1001', 'result' => $data]);
    }

    //预约借款 弹框界面
    public function user_debt()
    {
        $vid = I('get.id');
        $field = 'id,days,money,ctime,play_time,status';
        $data = M('user_appointment', '', 'CAIWU')->where(['vid' => $vid])->field($field)->order('ctime desc')->limit(0, 10)->select();
        foreach ($data as $k => $v) {
            $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
//            $data[$k]['play_time']=date('Y-m-d H:i',$v['play_time']);
            $data[$k]['status_s'] = $v['status'];
            if ($v['status'] == 1) {
                $data[$k]['status'] = '待打款';
            } else if ($v['status'] == 2) {
                $data[$k]['status'] = '打款成功';
            } else if ($v['status'] == 3) {
                $data[$k]['status'] = '打款失败';
            } else if ($v['status'] == 4) {
                $data[$k]['status'] = '已取消';
            } else if ($v['status'] == 5) {
                $data[$k]['status'] = '打款中';
            }

        }
        $this->assign(array(
            'data' => $data
        ));

        $this->display();
    }

    //罚金信息 弹框界面
    public function user_fine()
    {

        $id = I('get.id');
        $over = M('user_overdue', '', 'URGE')->where(['id' => $id])->field('vid,repayment_no')->find();
        $data = M('overdue_order', '', 'CAIWU')->where(['vid' => $over['vid'], 'repayment_no' => $over['repayment_no']])->field('repay_money,status,repay_time,real_time,type,overdue_no')->select();

        foreach ($data as $k => $v) {
            $data[$k]['repay_time'] = date('Y-m-d H:i', $v['repay_time']);
            if (empty($v['real_time'])) {
                $data[$k]['real_time'] = '-';
            } else {
                $data[$k]['real_time'] = date('Y-m-d H:i', $v['real_time']);
            }

            if ($v['status'] == '1') {
                $data[$k]['status'] = '待缴罚金';
            } else if ($v['status'] == '2') {
                $data[$k]['status'] = '还款中';
            } else if ($v['status'] == '3') {
                $data[$k]['status'] = '已收罚金';
            } else if ($v['status'] == '4') {
                $data[$k]['status'] = '还款失败';
            } else if ($v['status'] == '5') {
                $data[$k]['status'] = '取消还款';
            }

            if ($v['type'] == '1') {
                $data[$k]['type'] = '主动还款';
            } else if ($v['type'] == '2') {
                $data[$k]['type'] = '自动还款';
            } else if ($v['type'] == '3') {
                $data[$k]['type'] = '续借还款';
            } else if ($v['type'] == '4') {
                $data[$k]['type'] = '人工还款';
            }

        }


        $this->assign(array(
            'data' => $data
        ));

        $this->display();
    }

    /**
     * 人工调用用户运营商信息
     */
    public function mobile_cert()
    {
        $vid = I('vid');
        $cert_time = I('cert_time');

        $post_data = array(
            'vid' => $vid,
            'cert_time' => $cert_time
        );

        $ret = $this->send_post('https://adm.56gxxj.com/External/external_mobile_cert', $post_data);
        $json = json_decode($ret);
        $this->ajax($json->code, $json->result);
    }

    /**
     * 人工微信支付 弹框
     */
    public function wx_pay_view()
    {
        $codeUrl = I('get.url');
        $picurl = I('get.picurl');
        $this->assign(array(
            'url' => $codeUrl,
            'picurl' => $picurl
        ));

        $this->display();
    }

    /**
     * 人工微信支付
     */
    public function wx_pay()
    {
        $vid = I('vid');
        $repayment_no = I('repayment_no');

        $post_data = array(
            'vid' => $vid,
            'repayment_no' => $repayment_no
        );

        $ret = $this->send_post('https://adm.56gxxj.com/External/external_jh_wxpay', $post_data);
        $json = json_decode($ret);
        $this->ajax($json->code, $json->result);
    }

    /**
     * 发送post请求
     * @param string $url 请求地址
     * @param array $post_data post键值对数据
     * @return string
     */
    public function send_post($url, $post_data)
    {

        $postdata = http_build_query($post_data);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/x-www-form-urlencoded',
                'content' => $postdata,
                'timeout' => 15 * 60 // 超时时间（单位:s）
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }

    //补全信息 银行卡
    public function CompleteInformation()
    {
        $vid = I('vid');
        $bank_card = I('bank_card');

        $post_data = array(
            'vid' => $vid,
            'bank_card' => $bank_card
        );

        $ret = $this->send_post('https://adm.56gxxj.com/External/external_save_agree', $post_data);
        $json = json_decode($ret);
        $this->ajax($json->code, $json->result);
    }

    public function ajax($code, $result, $r = '')
    {
        if (empty($r)) {
            $this->ajaxReturn(['code' => $code, 'result' => $result]);
        } else {
            $this->ajaxReturn(['code' => $code, 'result' => $result]);
            return false;
        }
    }

    //优惠券列表 弹框界面
    public function coupon_list()
    {
        $vid = I('get.vid');
        $renewal = M('user_borrow', '', 'CAIWU');
        $w = [];
        $w['a.vid'] = array('eq', $vid);
        $tol = $renewal->join('b join user_coupon a on b.borrow_no=a.code')->where($w)->count();
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $data = $renewal->join('b join user_coupon a on b.coupon_id=a.id')->LIMIT($page->listfirst, $page->listRows)->where($w)->field('a.id,a.coupon_id,a.endtime,a.money,a.status')->order('a.id desc')->select();
        foreach ($data as $k => $v) {
            $data[$k]['coupon_name'] = M('coupon', '', 'CAIWU')->where(['id' => $v['coupon_id']])->getfield('name');
            if ($v['status'] == 1) {
                $data[$k]['status_a'] = '未使用';
            } else if ($v['status'] == 2) {
                $data[$k]['status_a'] = '已使用';
            } else if ($v['status'] == 3) {
                $data[$k]['status_a'] = '已过期';
            }
            if ($v['endtime'] > 0) {
                $data[$k]['endtime'] = date('Y-m-d H:i', $v['endtime']);
            } else {
                $data[$k]['endtime'] = '-';
            }

        }
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'tol' => $tol
        ));

        $this->display();
    }

    /**
     * 人工取消预约
     */
    public function cancel_apt()
    {
        $id = I('id');
        $post_data = array(
            'id' => $id
        );

        $ret = $this->send_post('https://adm.56gxxj.com/external/external_cancel_bespeak', $post_data);
        $json = json_decode($ret);
        $this->ajax($json->code, $json->result);
    }

    /**
     * 人工扣款
     */
    public function Debit()
    {
        $vid = I('vid');
        $post_data = array(
            'vid' => $vid,
        );
        $user = M('debit_record', '', 'KEFU')->where(['vid' => $vid])->find();
        if (empty($user)) {
            $data = array('vid' => $vid, 'updtime' => date('Y-m-d', time()), 'kefu_name' => $_SESSION['name']);
            M('debit_record', '', 'KEFU')->add($data);
            $ret = $this->send_post('https://adm.56gxxj.com/external/external_call_auto_repay', $post_data);
            $json = json_decode($ret);
            $this->ajax($json->code, $json->result);
        } else {
            $now = date('Y-m-d', time());
            if ($user['updtime'] == $now) {
                $this->ajax('1002', '一天只能调用一次');
            } else {
                M('debit_record', '', 'KEFU')->where(['vid' => $vid])->setfield('updtime', date('Y-m-d', time()));
                $ret = $this->send_post('https://adm.56gxxj.com/external/external_call_auto_repay', $post_data);
                $json = json_decode($ret);
                $this->ajax($json->code, $json->result);
            }
        }
    }

    /**
     *申请恢复额度
     */
    public function user_apply_money()
    {
        $vid = I('get.vid');
        if (empty($_POST)) {
            $this->assign(array(
                'vid' => $vid
            ));
            $this->display();
            die;
        }
        $vid = I('vid');
        $content = I('content');
        $reason = I('reason');

        //重复申请
        $info = M('applyfor_recheck', '', 'KEFU_FK')->where(['vid' => $vid, 'examine_status' => '1'])->find();
        if (!empty($info)) {
            $this->ajax('1002', '不可重复申请');
        }
        $arr = array('vid' => $vid, 'ctime' => time(), 'reson' => $reason, 'content' => $content, 'name' => $_SESSION['name']);
        $add = M('applyfor_recheck', '', 'KEFU_FK')->add($arr);
        if ($add) {
            $this->ajax('1001', '申请成功');
        } else {
            $this->ajax('1002', '申请失败');
        }
    }

    /**
     *申请恢复额度
     */
    public function apply_result()
    {
        $vid = I('vid');
        $add = M('applyfor_recheck', '', 'KEFU_FK')->where(['vid' => $vid])->order('id desc')->find();

        if (!empty($add)) {
            if ($add['examine_status'] == 1) {
                $status = '待审核';
            } else if ($add['examine_status'] == 2) {
                $status = '审核通过';
            } else if ($add['examine_status'] == 3) {
                $status = '申请被驳回！';
                $this->ajax('1003', $status);
                die;
            }
            $this->ajax('1001', $status);
        } else {
            $this->ajax('1002', '无申请');
        }
    }

    /**
     *减免罚息
     */
    public function reduce_fine()
    {
        $vid = I('get.vid');
        $repayment_no = I('get.repayment_no');
        $overdue_no = I('get.overdue_no');
        if (empty($_POST)) {
            $this->assign(array(
                'vid' => $vid,
                'repayment_no' => $repayment_no,
                'overdue_no' => $overdue_no
            ));
            $this->display();
            die;
        }
//        $vid = I('vid');
        $reduce_money = I('money');
        $reason = I('reason');
        $repayment_no = I('repayment_no');
        $overdue_no = I('overdue_no');

        //重复申请
        $apply = M('fine_modify_apply', '', 'URGE')->where(['repayment_no' => $repayment_no])->find();
        if (!empty($apply)) {
            $this->ajax('1002', '不可重复申请');
            die;
        }

        //判断申请金额有没有超过罚金
        $repay_money = M('overdue_order', '', 'CAIWU')->where(['overdue_no' => $overdue_no])->getfield('repay_money');
        if ($repay_money < $reduce_money) {
            $this->ajax('1002', '申请金额大于罚金');
            die;
        }

        $post_data = array(
            'repayment_no' => $repayment_no,
            'reduce_money' => $reduce_money,
            'reason' => $reason,
            'merchant_num' => 'dh_kefu',
            'urge_admin' => $_SESSION['name'],
        );

        $ret = $this->send_post('https://adm.56gxxj.com/External/external_apply_modify_fine', $post_data);
        $json = json_decode($ret);
        $this->ajax($json->code, $json->result);
    }

    /**
     *可申请减免罚息 逾期订单
     */
    public function overdue_list()
    {
        $vid = I('get.vid');
        $data = M('overdue_order', '', 'CAIWU')->where(['vid' => $vid, 'status' => '1'])->field('repay_money,repayment_no,overdue_no,repay_time')->order('id desc')->limit(1)->select();
        //是否存在已经申请
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $apply = M('fine_modify_apply', '', 'URGE')->where(['repayment_no' => $data[$k]['repayment_no']])->field('status')->find();
                if (empty($apply)) {
                    $data[$k]['check_status'] = '1';//没有申请过
                } else {
                    $data[$k]['check_status'] = '2';//申请过
                    switch ($apply['status']) {
                        case 1:
                            $data[$k]['check_result'] = '待审核';
                            break;
                        case 2:
                            $data[$k]['check_result'] = '审核通过';
                            break;
                        case 3:
                            $data[$k]['check_result'] = '驳回';
                            break;
                        case 4:
                            $data[$k]['check_result'] = '已修改';
                            break;
                    }
                }

            }
        }

        $this->assign(array(
            'data' => $data,
            'vid' => $vid
        ));
        $this->display();
        die;

    }

    //取消续借 弹框界面
    public function user_cancel_renew()
    {

        $vid = I('get.id');
        $w = [];
        $w['b.status'] = array('eq', '2');
        $w['b.vid'] = array('eq', $vid);
        $w['a.status'] = array('eq', '3');
        $renewal = M('repay_renewal', '', 'CAIWU');

        //可取消续借列表 必须满足已经还款和 续借已支付
        $data = $renewal->where($w)->join('b join dxd_caiwu.user_repayment a on b.repayment_no=a.repayment_no')->order('b.id desc')->limit(1)->field('a.real_time,b.repayment_no,b.orderno,b.changetime,b.money,b.type,b.ctime,b.status')->select();
        $arr = array();
        foreach ($data as $k => $v) {
            if (date('Y-m-d', $v['ctime']) == date('Y-m-d', $v['real_time'])) {
                if ($v['status'] == 1) {
                    $data[$k]['status_s'] = '待支付';
                } else if ($v['status'] == 2) {
                    $data[$k]['status_s'] = '已支付';
                } else if ($v['status'] == 3) {
                    $data[$k]['status_s'] = '删除';
                }
                if ($v['type'] == 1) {
                    $data[$k]['type'] = '正常续借';
                } else if ($v['type'] == 2) {
                    $data[$k]['type'] = '逾期续借';
                }

                $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);

                $check_status = M('renewal_refund_apply', '', 'KEFU')->where(['renewal_no' => $v['orderno'], 'vid' => $vid])->order('id desc')->field('status')->find();

                if (!empty($check_status)) {
                    $data[$k]['status'] = $check_status['status'] + 4;
                    if ($check_status['status'] == 1) {
                        $data[$k]['check'] = '待审核';
                    } else if ($check_status['status'] == 2) {
                        $data[$k]['check'] = '审核通过';
                    } else if ($check_status['status'] == 3) {
                        $data[$k]['check'] = '已驳回 重申请';
                    }
                }
                array_push($arr, $data[$k]);
            }
        }

//     var_dump($arr);die;
        $this->assign(array(
            'vid' => $vid,
            'data' => $arr,
        ));

        $this->display();

    }

    //取消续借
    public function renew_cancel_reson()
    {
        if (!$_POST) {
            $id = I('get.orderno');
            $vid = I('get.vid');
            $this->assign(array(
                'orderno' => $id,
                'vid' => $vid,
            ));
            $this->display();
            die;
        }
        $renewal = M('repay_renewal', '', 'CAIWU');
        $orderno = I('post.orderno');
        $vid = I('post.vid');
        $id_card = I('post.id_card');
        $reason = I('post.reason');
        //是否提交过
        $w = [];
        $w['vid'] = $vid;
        $w['renewal_no'] = array('eq', $orderno);
        $w['status'] = array('in', array('1', '2'));
        $find = M('renewal_refund_apply', '', 'KEFU')->where($w)->find();
        if (!empty($find)) {
            $this->ajax('1002', '已经申请');
            die;
        }
        $user_idcard = M('user', '', 'USER')->where(['vid' => $vid])->getfield('id_card');
        $last4 = substr($user_idcard, count($user_idcard) - 5, 4);
        if ($last4 != $id_card) {
            $this->ajax('1003', '身份证信息不对');
            die;
        }
        //续借费用

        $repay = $renewal->where(['orderno' => $orderno])->field('money,repayment_no,type')->find();
        if ($repay['type'] == 2) {
//            $fine = M('overdue_order', '', 'CAIWU')->where(['repayment_no' => $repay['repayment_no']])->getfield('repay_money');
            $fine = M('overdue_order', '', 'CAIWU')->where(['renewal_order' => $orderno])->getfield('repay_money');
            $repay['money'] = round($repay['money'] - $fine, 2);
        }
        $arr = array('vid' => $vid, 'reason' => $reason, 'ctime' => time(), 'name' => $_SESSION['name'], 'renewal_no' => $orderno, 'money' => $repay['money']);
        $add = M('renewal_refund_apply', '', 'KEFU')->add($arr);
        if ($add) {
            $this->ajax('1001', '提交成功');
            die;
        }
        $this->ajax('1002', '提交失败');
        die;
    }

    /**
     * 优惠券派送 客服只派送亲亲券  面值20  id 7
     */
    public function coupon_send()
    {
        $vid = I('vid');
        //优惠券是否能发放
        $status = M('coupon', '', 'CAIWU')->where(['id' => '7'])->getfield('status');
        if ($status == '2'){
            $this->ajax('1002', '无优惠券可送！');
            die;
        }

        //是否送过
        $exit = M('user_coupon', '', 'CAIWU')->where(['vid' => $vid, 'coupon_id' => '7'])->find();
        if (!empty($exit)) {
            $this->ajax('1002', '该用户已经送过亲亲券！');
            die;
        }

        $arr = array('vid' => $vid, 'coupon_id' => '7', 'status' => '1', 'money' => '20', 'ctime' => time(), 'endtime' => time() * 70 * 365 * 24 * 3600);
        if (M('user_coupon', '', 'CAIWU')->add($arr)) {
            //用户手机
            $phone = M('user', '', 'USER')->where(['vid' => $vid])->getfield('mobile');
            send_sms($phone, '很抱歉给您造成不便！作为补偿，20元现金优惠券已放入您的账户【共享现金】');
            $this->ajax('1001', '赠送成功');
            die;
        }
        $this->ajax('1002', '赠送失败');
        die;
    }

    //工作认证
    public function work_cert(){
        $vid=I('vid');
        $data=M('user_woke_verify','','USER')->where(['vid'=>$vid])->field('work_mobile,work_address,work_position,work_company,img_one,img_thr,img_two,monthly_income,ctime')->find();
//       var_dump($data);die;
         $this->assign(array(
             'data'=>$data,
             'vid'=>$vid
         ));
        $this->display();

    }

    /**
     * 修改工作图片
     */
    public function update_user_workpic()
    {
        $vid=I('vid');
        $this->assign(array(
            'vid'=>$vid
        ));
        $this->display();
    }

    /**
     * 修改工作图片
     */
    public function update_workpic()
    {
        $one=I('one');
        $two=I('two');
        $thr=I('thr');
        $vid=I('vid');

        if(!empty($one)){
            $update=M('user_woke_verify','','USER')->where(['vid'=>$vid])->setField('img_one',$one);
        }
        if(!empty($two)){
            $update=M('user_woke_verify','','USER')->where(['vid'=>$vid])->setField('img_two',$two);
        }
        if(!empty($thr)){
            $update=M('user_woke_verify','','USER')->where(['vid'=>$vid])->setField('img_thr',$thr);
        }

        if($update){
            $this->ajax('1001', '修改成功');
            die;
        }
        $this->ajax('1002', '修改失败');
        die;
    }




}